﻿/**************************************************************************
创建时间:	2020/4/14 14:15:16    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZhCun.Win.Controls
{
    /// <summary>
    /// 时间编辑框
    /// </summary>
    public class EditTime : EditText
    {
        public EditTime()
        {

            MaxLength = 5;
            ShortcutsEnabled = false;
            this.TextAlign = HorizontalAlignment.Center;
            base.Text = "00:00";

            //this.ResumeLayout(false);
            //this.PerformLayout();
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (!DesignMode)
                {
                    base.Text = value;
                }
            }
        }

        bool InputCheck(char keyChar)
        {
            //char keyChar = (char)e.KeyCode;

            if ((keyChar >= '0' && keyChar <= '9'))
            //不输入输入除了数字之外的所有非法字符的判断
            {
                return !(
                    (SelectionStart == 0 && keyChar > '2') ||
                    (SelectionStart == 1 && Text[0] == '2' && keyChar > '3') ||
                    (SelectionStart == 3 && keyChar > '5') ||
                    (SelectionStart == 4 && keyChar > '9')
                    );

            }
            return false;
        }

        void SetTextChar(char keyData)
        {
            char[] txtChars = new char[5];
            for (int i = 0; i < 5; i++)
            {
                txtChars[i] = Text[i];
                if (SelectionStart == i)
                {
                    txtChars[i] = keyData;
                }
            }
            if (SelectionStart == 0 && Text[1] > '3')
            {
                txtChars[1] = '0';
            }
            int selStart = SelectionStart;
            Text = new string(txtChars);
            Select(selStart, 1);
        }

        void SetNextValue(int nextValue)
        {
            if (SelectionStart < 2)
            {
                //时
                int h = int.Parse(Text.Substring(0, 2));
                h += nextValue;
                if (h > 23) h = 0;
                if (h < 0) h = 23;
                Text = $"{h:D2}{Text.Substring(2, 3)}";
                Select(0, 2);
            }
            else
            {
                //分
                int m = int.Parse(Text.Substring(3, 2));
                m += nextValue;
                if (m > 59) m = 0;
                if (m < 0) m = 59;
                Text = $"{Text.Substring(0, 3)}{m:D2}";
                Select(3, 2);
            }
        }

        protected override void OnEnter(EventArgs e)
        {
            Select(0, 1);

            base.OnEnter(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            int selStart = base.SelectionStart;
            if (selStart == 2)
            {
                selStart = 3;
            }
            Select(selStart, 1);
            base.OnMouseClick(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Delete)
            {
                SetTextChar('0');
            }

            if (e.KeyCode == Keys.Back && SelectionStart > 0)
            {
                if (SelectionStart == 3) SelectionStart = 2;
                SelectionStart--;
                SetTextChar('0');
            }

            if (e.KeyCode == Keys.Up)
            {
                e.Handled = true;
                SetNextValue(1);
            }
            if (e.KeyCode == Keys.Down)
            {
                e.Handled = true;
                SetNextValue(-1);
            }

            base.OnKeyDown(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!InputCheck(e.KeyChar))
            {
                e.Handled = true;
            }
            else
            {
                SetTextChar(e.KeyChar);
            }

            base.OnKeyPress(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left && SelectionStart > 0)
            {
                if (SelectionStart == 3) SelectionStart = 2;
                SelectionStart--;
            }
            else if (SelectionStart == 2)
            {
                SelectionStart = 3;
            }

            Select(SelectionStart, 1);

            base.OnKeyUp(e);
        }

        protected override void OnLeave(EventArgs e)
        {
            Tag = Value;
            base.OnLeave(e);
        }
        /// <summary>
        /// 获取时间的分钟数
        /// </summary>
        public int Hour
        {
            get
            {
                return int.Parse(Text.Substring(0, 2));
            }
        }
        /// <summary>
        /// 获取时间的分钟数
        /// </summary>
        public int Minutes
        {
            get
            {
                return int.Parse(Text.Substring(3, 2));
            }
        }
        /// <summary>
        /// 获取时间TimeSpan，ToString方法format格式：@"hh\:mm";
        /// </summary>
        public TimeSpan Value
        {
            get
            {
                return new TimeSpan(Hour, Minutes, 0);
            }
            set
            {
                Text = value.ToString(@"hh\:mm");
            }
        }
        /// <summary>
        /// 实际值，等同于 Value
        /// </summary>
        public override object RealValue
        {
            get { return Value; }
            set
            {
                if (TimeSpan.TryParse(value.ToString(), out TimeSpan ts))
                {
                    Value = ts;
                }
            }
        }

        /// <summary>
        /// 从tag中获取值复制到Text中
        /// </summary>
        public void SetTextByTag()
        {
            if (Tag is TimeSpan ts)
            {
                Text = ts.ToString(@"hh\:mm");
            }
        }
    }
}
